package com.to8to.scene.stat;

import static com.mongodb.QueryOperators.IN;
import static com.to8to.scene.server.Commons.mongo;
import static com.to8to.thrift.diary.consts.constConstants.ENTITY_STATUS_NORM;
import static com.to8to.thrift.diary.consts.constConstants.TARGET_TYPE_COMMENT;
import static com.to8to.thrift.diary.consts.constConstants.TARGET_TYPE_DIARY;
import static com.to8to.thrift.diary.consts.constConstants.TARGET_TYPE_PRODUCT;
import static com.to8to.thrift.diary.consts.constConstants.TARGET_TYPE_SCENE;
import static com.to8to.thrift.diary.module.TSceneStat._Fields.RECENT_EVENT;
import static com.to8to.thrift.diary.module.TSceneStat._Fields.RECENT_TIME;
import static com.to8to.thrift.diary.module.TSceneStat._Fields.RECENT_USER;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.to8to.scene.server.Tables;
import com.to8to.thrift.diary.consts.constConstants;
import com.to8to.thrift.diary.module.TComment;

public abstract class AbstractStatUpdate implements StatUpdateIface
{

	public static Logger logger = LoggerFactory.getLogger(AbstractStatUpdate.class);
    /**
     * 设置最近信息
     * 
     * @param set
     * @param recentEvent
     *            最件事件
     * @param recentTime
     *            最近时间
     * @param userId
     *            发起用户
     */
    public void setRecent(DBObject set, int recentEvent, int recentTime, String userId)
    {
        set.put(STAT_PREFIX + RECENT_EVENT.getFieldName(), recentEvent);
        set.put(STAT_PREFIX + RECENT_TIME.getFieldName(), recentTime);
        set.put(STAT_PREFIX + RECENT_USER.getFieldName(), userId);
    }

    public void refreshCommentNum(String tableName, String tgt_id, int recentTime, String userId, boolean setRecent, boolean setCommentNum,
            boolean withComment, String comment_field,List<Integer> target_type_list)
    {
        if (!setRecent && !setCommentNum) //既不刷新最近时间，也不刷新回复数，则直接返回
            return;

        DBObject set = new BasicDBObject();
        if (setCommentNum)
        {
            //          回复的回复又要统计(20141204 modify by alpha.ren)
            // 查询出评论数：回复的回复不统计，删除的回复不统计(20141106 modify by atom.liu)
//            List<Integer> target_type_list = Arrays.asList(TARGET_TYPE_SCENE, TARGET_TYPE_DIARY, TARGET_TYPE_PRODUCT);
            if (withComment)
            	target_type_list.add(TARGET_TYPE_COMMENT);
//                target_type_list = Arrays.asList(TARGET_TYPE_SCENE, TARGET_TYPE_DIARY, TARGET_TYPE_PRODUCT, TARGET_TYPE_COMMENT);
            DBObject cond = new BasicDBObject(tableName + "_id", tgt_id).append("tgt_type", new BasicDBObject(IN, target_type_list));
            cond.put(TComment._Fields.STATUS.getFieldName(), ENTITY_STATUS_NORM);
            int comments = mongo.count(Tables.Comment, cond);
            set.put(comment_field, comments);
        }

        // 设置最近的信息
        if (setRecent)
            setRecent(set, constConstants.RECENT_EVENT_COMMENT, recentTime, userId);

        DBObject oper = new BasicDBObject("$set", set);
        DBObject updateCond = new BasicDBObject("_id", tgt_id);

        mongo.update(tableName, updateCond, oper, false, false);
        logger.debug("update comment {} on {}",oper,updateCond);
    }

    public static String getString(Object obj)
    {
        return obj == null ? null : obj.toString();
    }

    public static Integer getInteger(Object obj)
    {
        if (obj == null)
        {
            return 0;
        }
        String str = getString(obj);
        str = StringUtils.substringBefore(str, ".");
        return obj == null ? 0 : Integer.parseInt(str);
    }

}
